package com.canyou.integration.authenticator;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.canyou.integration.IntegrationAuthentication;
import com.canyou.system.model.User;
import com.canyou.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

/**
 * 默认认证处理
 *
 * @author cgz
 * @date 2019-3-31
 **/
@Component
@Primary
public class UserPwdAuthenticator extends AbstractIntegrationAuthenticator {

    @Autowired
    private UserService userService;

    @Override
    public User authenticate(IntegrationAuthentication integrationAuthentication) {
        String username = integrationAuthentication.getUsername();
        return userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username), false);
    }

    @Override
    public void prepare(IntegrationAuthentication integrationAuthentication) {
    }

    @Override
    public boolean support(IntegrationAuthentication integrationAuthentication) {
        return StrUtil.isEmpty(integrationAuthentication.getAuthType());
    }
}
